package leetcode.leetcode100;

public class maxSubArray {
    public static void main(String[] args) {

        int[] nums = {5,4,-1,7,8};
        System.out.println(process(nums));
    }
    public static int process(int[] nums){
        int max = nums[0];

        int[] ans = new int[nums.length];

        ans[0] = nums[0] > 0 ? nums[0] : 0;

        for (int i = 1; i < nums.length; i++) {

            if (nums[i] + ans[i-1] >= 0){
                ans[i] =  nums[i] + ans[i-1];
                max = Math.max(max,ans[i]);
            }else {
                ans[i] = 0;
                max = Math.max(max,nums[i]);
            }
        }

        return max;
    }

}
